<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use App\Models\Agent;
use Encore\Admin\Form\Tab;


class AgentController extends Controller
{
	use HasResourceActions;
	
	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content)
	{		
		return Admin::content(function (Content $content) {			
			$content->header('代理商列表');
			$content->description('代理商管理');			
			$content->body($this->grid());
		});
	}
	
	/**
	 * Show interface.
	 *
	 * @param mixed   $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content)
	{
		return $content->header('代理商')
		->description('公司详情')
		->body(Admin::show(Agent::findOrFail($id), function (Show $show) {
		
			//$show->id('序号')->sortable();
			$show->panel()->title('公司信息');	
			$show->company_name('公司名称');
			$show->company_type('公司类型')
			->using([
					Agent::TYPE_AGENT=>Agent::TYPE_AGENT_STRING,
					Agent::TYPE_DIRECT=>Agent::TYPE_DIRECT_STRING,
					Agent::TYPE_ORDINARY=>Agent::TYPE_ORDINARY_STRING
			]);
			
			$show->address('公司地址');
			$show->join_date入('入驻时间');
			$show->divider();
			
			$show->linkman('人员信息',function($linkman){
				$linkman->panel()
				->tools(function ($tools) {
					$tools->disableEdit();
					$tools->disableList();
					$tools->disableDelete();
				});;
				
				$linkman->legal_person('法人姓名');
				$linkman->legal_person_phone('法人电话');
				$linkman->linkman_name('联系人姓名');
				$linkman->linkman_phone('联系人电话');
			});
			
			
			$show->customer_num('财务公司数量');
			$show->balance('平台余额(金豆)');
			
			
			$show->account_type('合作状态');

			
					

		}));
	}
	
	/**
	 * Edit interface.
	 *
	 * @param mixed   $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content)
	{
		return Admin::content(function (Content $content) use ($id) {
			
			$content->header('代理商信息修改');
			$content->description('代理商管理');
			
			$content->body($this->form()->edit($id));
		});
	}
	
	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content)
	{
		return Admin::content(function (Content $content) {
			
			$content->header('创建代理商');
			$content->description('代理商管理');
			
			$content->body($this->form());
		});
	}
	
	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid()
	{			
		return Admin::grid(Agent::class, function (Grid $grid) {
// 			$grid->actions(function ($actions) {				
// 				$actions->disableView();
// 				$actions->disableDelete();
// 				$actions->disableEdit();
// 				$actions->append('<a href="#" class="btn btn-primary btn-sm" role="button">客户详情</a>&nbsp;&nbsp;');
// 				//$actions->append('<a href="#" class="btn btn-primary btn-sm" role="button">停用</a>&nbsp;&nbsp;');
// 				$actions->append('<a href="#" class="btn btn-primary btn-sm" role="button">查看账单明细</a>&nbsp;&nbsp;');
// 			});
			
			$grid->id('序号')->sortable();
			$grid->company_name('公司名称');
			$grid->address('所在地');
			$grid->customer_num('财务公司数量');
			$grid->balance('平台余额(金豆)');
			$grid->join_date('入驻时间');
			$grid->company_state('合作状态')->select(Agent::getStateDispayMap());
			

			
			$grid->filter(function ($filter) {
				$filter->like('company_name', '公司名称');			
				$filter->notEqual('account_type', '合作状态')->select(Agent::getStateDispayMap());
				$filter->between('join_date','入驻时间')->datetime();
			});
			
		}); 
		


		
	}
	

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form()
	{
	    return Admin::form(Agent::class, function(Form $form){
	    	$form->tab('公司信息', function (Form $form) {
	    		$form->text('company_name','公司名称')->rules('required');	    		     
	    		$form->text('address','公司通讯地址')->rules('required');
	    		$form->select('company_type','公司类型')->options(Agent::getTypeDispayMap())->default(Agent::TYPE_AGENT);	
	    		$form->datetime('join_date','入驻时间');    		
	    		 
	    	})->tab('人员信息',function(Form $form){
	    		$form->text('linkman.legal_person','法人姓名');
	    		$form->text('linkman.legal_person_phone','法人联系电话');
	    		$form->text('linkman.linkman_name','联系人姓名');
	    		$form->text('linkman.linkman_phone','联系人电话');

	    	})->tab('登录账号',function(Form $form){
	    		$form->html('<div style="margin-top:15px;">友情提示：<span style="color:#f39c12">此账号即可以登录木牛盒子也可登录计费后台。</span></div>', $label = '');
	            $form->text('sysuser.login_name','账号');
	    		$form->text('sysuser.password','密码');
	    		
	    	})->tab('平台余额信息',function(Form $form){
	    		$form->display('balance','金豆余额');
	    		
	    	})->tab('财务公司数量',function(Form $form){
	    		$form->text('customer_num','财务公司总数');
	    		
	    		
	    	})->tab('充值设置',function(Form $form){
	    		$form->text('ratio','兑换比例');
	    		
	    	})->tab('使用状态',function(Form $form){
	    		$form->radio('company_state','合作状态')->options(Agent::getStateDispayMap())->default(Agent::STATE_NORMAL);
	    	});	
			
	    });
			
	    
	}
	
}
